@echo off

rem Get Admin Auth
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' NEQ '0' (goto UACPrompt) else (goto UACAdmin)
:UACPrompt
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
:UACAdmin
echo Get Admin Auth Sucess!

set install_path=E:\install_path
if not exist %install_path% md %install_path%

set system_regedit_path="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
set sys_var_path=Path


rem download packages path
set download_path=E:\packages_download
if not exist %download_path% md %download_path%

rem ===============================JDK=============================================================
rem Install JDK
set remote_jdk_package=https://download.oracle.com/java/20/latest/jdk-20_windows-x64_bin.zip
set remote_jdk_package_sha256=%remote_jdk_package%.sha256
set local_jdk_name=jdk.zip
set local_jdk_sha256=jdk256.txt

rem Download JDK packages
echo [Info]Installing JDK...
echo [Info]Download JDK packages...
if not exist %download_path%\%local_jdk_sha256% (
    curl -s -o %download_path%\%local_jdk_sha256% %remote_jdk_package_sha256%
    if not %errorlevel% == 0 (
        echo [Error]Download JDK Error: cannot get SHA256, abort!!!
        exit /b %errorlevel%
    )
)

if not exist %download_path%\%local_jdk_name% (
    curl --progress-bar -o %download_path%\%local_jdk_name% %remote_jdk_package%
    if not %errorlevel% == 0 (
        echo [Error]Download JDK Error, abort!!!
        exit /b %errorlevel%
    )
)

rem compare sha256
set result="NOK"
for /f "delims=" %%a in (%download_path%\%local_jdk_sha256%) do set hash256_cmp=%%a
for /f "delims=" %%a in ('certutil -hashfile %download_path%\%local_jdk_name% SHA256') do (
    if "%hash256_cmp%" == "%%a" set result="OK"
)

if %result% == "OK" (
    echo [Info]Download JDK packages Sucess
) else (
    echo [Info]HASH check error, re-download...
    curl --progress-bar -o %download_path%\%local_jdk_name% %remote_jdk_package%
    if not %errorlevel% == 0 (
        echo [Error]re-Download JDK Error, abort!!!
        exit /b %errorlevel%
    )

    set result="NOK"
    for /f "delims=" %%a in ('certutil -hashfile %download_path%\%local_jdk_name% SHA256') do (
        if "%hash256_cmp%" == "%%a" set result="OK"
    )
    if not %result% == "OK" (
        echo [Error]HASH re-check error, abort!!!
        exit /b 1
    )
    echo [Info]Download JDK packages Sucess
)

rem use WinRAR decompress, suppose in "C:\Program Files\WinRAR\winrar.exe"
set winrar_exe="C:\Program Files\WinRAR\winrar.exe"
set jdk_home_name=jdk

echo [Info]Decompress JDK packages...
%winrar_exe% X %download_path%\%local_jdk_name% %install_path%
if not %errorlevel% == 0 (
    echo [Error]Decompress JDK packages error, abort!!!
    exit /b %errorlevel%
)
pushd %install_path%
for /f "tokens=4 delims= " %%a in ('dir jdk-* ^| findstr jdk') do (
    ren %%a %jdk_home_name%
    if not %errorlevel% == 0 (
        echo [Error]Move JDK dir error, abort!!!
        exit /b %errorlevel%
    )
)
popd
echo [Info]Decompress JDK packages Sucess

rem add env JAVA_HOME:%install_path%\%jdk_home_name%, add JAVA_HOME to Path
setx JAVA_HOME "%install_path%\%jdk_home_name%" /m
if not %errorlevel% == 0 (
    echo [Error] error:%install_path%\%jdk_home_name%, abort!!!
    exit /b %errorlevel%
)
echo [Info]Set JAVA_HOME OK:%install_path%\%jdk_home_name%


set PATH=%PATH%;%install_path%\%jdk_home_name%\bin
java -version 1>nul 2>nul
if not %errorlevel% == 0 (
    echo [Warnning]run java -version fail
)
echo [Info]JDK installation finish


rem ===============================Android SDK=============================================================
echo [Info]Install Android SDK...

set androidsdk_home=%install_path%\androidsdk
if not exist %androidsdk_home% md %androidsdk_home%

echo [Info]Download Android SDK cmd tools
set remote_androisdk_package=https://dl.google.com/android/repository/commandlinetools-win-9477386_latest.zip
set remote_androisdk_package_sha256=696431978daadd33a28841320659835ba8db8080a535b8f35e9e60701ab8b491
set local_androidsdk_package=androidsdk.zip
if not exist %download_path%\%local_androidsdk_package% (
    curl --progress-bar -o %download_path%\%local_androidsdk_package% %remote_androisdk_package%
    if not %errorlevel% == 0 (
        echo [Error]ownload Android SDK cmd tools err, abort!!!
        exit /b %errorlevel%
    )
)
set result="NOK"
for /f "delims=" %%a in ('certutil -hashfile %download_path%\%local_androidsdk_package% SHA256') do (
    if "%remote_androisdk_package_sha256%" == "%%a" set result="OK"
)
if not %result% == "OK" (
    echo [Error]Android SDK cmd tools hash check error, abort!!!
    del /q %download_path%\%local_androidsdk_package%
    exit /b 1
)
echo [Info]Download Android SDK cmd tools Sucess

echo [Info]Decompress Android SDK cmd tools...
%winrar_exe% X %download_path%\%local_androidsdk_package% %androidsdk_home%
if not %errorlevel% == 0 (
    echo [Error]Decompress Android SDK cmd tools error, abort!!!
    exit /b %errorlevel%
)
echo [Info]Decompress Android SDK cmd tools Sucess

echo [Info]Android SDK cmd tools installation...
rem %androidsdk_home%\cmdline-tools create %androidsdk_home%\cmdline-tools\latest
rem move %androidsdk_home%\cmdline-tools\bin %androidsdk_home%\cmdline-tools\lib %androidsdk_home%\cmdline-tools\NOTICE.txt %androidsdk_home%\cmdline-tools\source.properties
rem to %androidsdk_home%\cmdline-tools\latest
md %androidsdk_home%\cmdline-tools\latest
move %androidsdk_home%\cmdline-tools\bin %androidsdk_home%\cmdline-tools\latest
move %androidsdk_home%\cmdline-tools\lib %androidsdk_home%\cmdline-tools\latest
move %androidsdk_home%\cmdline-tools\NOTICE.txt %androidsdk_home%\cmdline-tools\latest
move %androidsdk_home%\cmdline-tools\source.properties %androidsdk_home%\cmdline-tools\latest
if not %errorlevel% == 0 (
    echo [Error]Android SDK cmd tools move error, abort!!!
    exit /b %errorlevel%
)
echo [Info]Install Android SDK cmd tools Sucess

setx ANDROID_HOME "%androidsdk_home%%" /m

echo [Info]Download "tools" "platform-tools" "build-tools;33.0.1" "platforms;android-33" "system-images;android-33;google_apis;x86_64"
echo y>rd_input_y_file.tmp

set sdkmanager=%androidsdk_home%\cmdline-tools\latest\bin\sdkmanager.bat
echo [Info]Download "tools"
call %sdkmanager% "tools" <rd_input_y_file.tmp
if not %errorlevel% == 0 (
    echo [Warnning]Download "tools" error, manual excute: %sdkmanager% "tools" or %sdkmanager% "tools" --proxy=XXX --proxy_port=YYY
) else (
    echo [Info]Download "tools" Sucess
)

echo [Info]Download "platform-tools"
call %sdkmanager% "platform-tools" <rd_input_y_file.tmp
if not %errorlevel% == 0 (
    echo [Warnning]Download "platform-tools" error, manual excute: %sdkmanager% "platform-tools" or %sdkmanager% "platform-tools" --proxy=XXX --proxy_port=YYY
) else (
    echo [Info]Download "platform-tools" Sucess
)

echo [Info]Download "build-tools;33.0.1"
call %sdkmanager% "build-tools;33.0.1" <rd_input_y_file.tmp
if not %errorlevel% == 0 (
    echo [Warnning]Download "build-tools;33.0.1" error, manual excute: %sdkmanager% "build-tools;33.0.1" or %sdkmanager% "build-tools;33.0.1" --proxy=XXX --proxy_port=YYY
) else (
    echo [Info]Download "build-tools;33.0.1" Sucess
)

echo [Info]Download "platforms;android-33"
call %sdkmanager% "platforms;android-33" <rd_input_y_file.tmp
if not %errorlevel% == 0 (
    echo [Warnning]Download "platforms;android-33" error, manual excute: %sdkmanager% "platforms;android-33"or %sdkmanager% "platforms;android-33" --proxy=XXX --proxy_port=YYY
) else (
    echo [Info]Download "platforms;android-33" Sucess
)

echo [Info]Download "system-images;android-33;google_apis;x86_64"
call %sdkmanager% "system-images;android-33;google_apis;x86_64" <rd_input_y_file.tmp
if not %errorlevel% == 0 (
    echo [Warnning]Download "system-images;android-33;google_apis;x86_64"error, manual excute: %sdkmanager% "system-images;android-33;google_apis;x86_64" or %sdkmanager% "system-images;android-33;google_apis;x86_64" --proxy=XXX --proxy_port=YYY
) else (
    echo [Info]Download "system-images;android-33;google_apis;x86_64" Sucess
)

del /q rd_input_y_file.tmp

set PATH=%PATH%;%androidsdk_home%;%androidsdk_home%\platform-tools;%androidsdk_home%\emulator
adb --version 1>nul 2>nul
if not %errorlevel% == 0 (
    echo [Warnning]run adb --version fail
)
echo [Info]Finish Android SDK installation


rem ===============================node.js and npm=============================================================
echo [Info]Install node.js/npm...

echo [Info]Download Node.js...
set remote_nodejs_package=https://nodejs.org/dist/v18.17.0/node-v18.17.0-win-x64.zip
set remote_nodejs_package_sha256=06e30b4e70b18d794651ef132c39080e5eaaa1187f938721d57edae2824f4e96
set local_nodejs_package=nodejs.zip
set nodejs_home_name=nodejs

if not exist %download_path%\%local_nodejs_package% (
    curl --progress-bar -o %download_path%\%local_nodejs_package% %remote_nodejs_package%
    if not %errorlevel% == 0 (
        echo [Error]Download nodejs error, abort!!! Please download from https://nodejs.org/dist/v18.17.0/node-v18.17.0-win-x64.zip
        exit /b %errorlevel%
    )
)
set result="NOK"
for /f "delims=" %%a in ('certutil -hashfile %download_path%\%local_nodejs_package% SHA256') do (
    if "%remote_nodejs_package_sha256%" == "%%a" set result="OK"
)
if not %result% == "OK" (
    echo [Error]nodejs hash check fail, abort!!!
    del /q %download_path%\%local_nodejs_package%
    exit /b 1
)
echo [Info]Download nodejs Sucess

echo [Info]Decompress nodejs...
%winrar_exe% X %download_path%\%local_nodejs_package% %install_path%
if not %errorlevel% == 0 (
    echo [Error]Decompress nodejs fail, abort!!!
    exit /b %errorlevel%
)
pushd %install_path%
for /f "tokens=4 delims= " %%a in ('dir node-v* ^| findstr node') do (
    ren %%a %nodejs_home_name%
    if not %errorlevel% == 0 (
        echo [Error]Move Nodejs error, abort!!!
        exit /b %errorlevel%
    )
)
popd
echo [Info]Decompress Nodejs Sucess

set PATH=%PATH%;%install_path%\%nodejs_home_name%
call npm -v 1>nul 2>nul
if not %errorlevel% == 0 (
    echo [Warnning]run npm -v fail
)
echo [Info]Finish nodejs installation



rem ===============================Appium server=============================================================
echo [Info]Install Appium...
call npm i -g appium 1>nul 2>nul
call appium -v 1>nul 2>nul
if not %errorlevel% == 0 (
    echo [Warnning]Install Appium error, please run npm i -g appium
) else (
    echo [Info]Install Appium Sucess, now install Android driver uiautomator2...
    call appium driver install uiautomator2 1>nul 2>nul
    if not %errorlevel% == 0 (
        echo [Warnning]Install uiautomator2 error, please run "appium driver install uiautomator2"
    ) else (
        echo [Info]Install uiautomator2 Sucess
    )
)


rem ===============================Create AVD=============================================================
set avdmanager=%androidsdk_home%\cmdline-tools\latest\bin\avdmanager.bat
echo no>rd_input_no_file.temp
call %avdmanager% create avd -n "test_phone" -k "system-images;android-33;google_apis;x86_64" <rd_input_no_file.temp
if %errorlevel% == 0 (
    echo [Info]Create AVD, name: test_phone, use "emulator -avd test_phone" start it
)


rem ============================PATH EVN================================================================
rem setx requires length of paht<1024
reg query %system_regedit_path% /v %sys_var_path% 1>nul 2>nul
if %errorlevel% == 0 (
    for /f "tokens=3 delims= " %%a in ('reg query %system_regedit_path% /v %sys_var_path%') do set old_sys_path=%%a
) else (
    set old_sys_path="="
)

echo [Info]Path: %old_sys_path%
set add_sys_env_path=;

rem add JAVA_HOME to Path
echo "%old_sys_path%" | findstr "JAVA_HOME"
if not %errorlevel% == 0 (
    set add_sys_env_path=%add_sys_env_path%;^%%JAVA_HOME^%%\bin
)

rem add ANDROID_HOME to Path
echo "%old_sys_path%" | findstr "ANDROID_HOME"
if not %errorlevel% == 0 (
    set add_sys_env_path=%add_sys_env_path%;^%%ANDROID_HOME^%%;^%%ANDROID_HOME%^%\platform-tools;^%%ANDROID_HOME%^%\emulator
)

rem add nodejs to Path
echo "%old_sys_path%" | findstr "%install_path%\%nodejs_home_name%"
if not %errorlevel% == 0 (
    set add_sys_env_path=%add_sys_env_path%;%install_path%\%nodejs_home_name%
)

if "%old_sys_path%" == ""="" (
    setx %sys_var_path% "%add_sys_env_path%" /m
) else (
    setx %sys_var_path% "%old_sys_path%;%add_sys_env_path%" /m
)

rem ============================clean files================================================================
del /q %download_path%\%local_jdk_sha256%
rem del /q %download_path%\%jdk_home_name%


rem ============================Final================================================================
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo Next, you can:
echo use "appium" to start Appium Server
echo use "emulator -avd test_phone" to start a AVDS
echo use https://github.com/appium/appium-inspector/releases for elements locate
echo use Python write usecase
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

pause